using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace Ordina.SharePoint.RemoveShareFollow.Features.FeatureMasterPage
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("a66fbea2-6b5f-4ab7-9091-2cb4c160452c")]
    public class FeatureMasterPageEventReceiver : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPSite siteCollection = properties.Feature.Parent as SPSite;
            if (siteCollection != null)
            {
                SPWeb topLevelSite = siteCollection.RootWeb;

                // calculate relative path of site from Web Application root
                string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
                if (!WebAppRelativePath.EndsWith(@"/"))
                {
                    WebAppRelativePath += @"/";
                }

                foreach (SPWeb site in siteCollection.AllWebs)
                {
                    site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/RemoveFS.master";
                    site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/RemoveFS.master";
                    site.AlternateCssUrl = WebAppRelativePath + "Style%20Library/RemoveFS/Styles.css";
                    site.SiteLogoUrl = WebAppRelativePath + "Style%20Library/RemoveFS/Images/SITEICON.PNG";
                    //site.UIVersion = 4;
                    //site.UIVersionConfigurationEnabled = false;
                    site.Update();
                }

            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = properties.Feature.Parent as SPSite;
            if (siteCollection != null)
            {
                SPWeb topLevelSite = siteCollection.RootWeb;

                // calculate relative path of site from Web Application root
                string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
                if (!WebAppRelativePath.EndsWith(@"/"))
                {
                    WebAppRelativePath += @"/";
                }

                foreach (SPWeb site in siteCollection.AllWebs)
                {
                    site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/seattle.master";
                    site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/seattle.master";
                    site.AlternateCssUrl = "";
                    site.SiteLogoUrl = "";
                    //site.UIVersion = 4;
                    site.Update();
                }
            }
        }
    }
}
